<?php
class ProductCatalogAction extends CommonAction {

	//private variable indexPath = "/Tpl/default/Product/Product_catalog/index.html" ;
	
	public $table = "product_catalog" ; 
	
	
	
	//框架首页
	public function index() {
		//按顺序查询产品类型				
		$model = D ( "product_type" );
		$condition['status'] = 1 ;
		$typeList = $model->where($condition)->order('statusdate desc')->select();		
		$this->assign("typeList",$typeList);
		//循环取出产品类型		
		$containList ;
		$temp_typeList  ;
		for ($i= 0;$i<count($typeList); $i++){
			$tempList = $typeList[$i];		
					
			$containList[$i] = $this->getSubFolder(0,$tempList['id'],$i) ;			
			//$temp_typeList[$i] = "<a href='__URL__/addCatalog/parentid/0/type_id/" .$typeList[$i]['id']. "'  target='dialog' mask='true' width='400' height='200'><span>新增产品目录</span></a>";			
			//如果没有树子结点	
			/*
			if(empty($containList[$i])){
				$containList[$i] = $typeList[$i]['id'] ;				
				$containList[$i] = "<a href='__URL__/addCatalog/parentid/0/type_id/" .$typeList[$i]['id']. "'  target='dialog' mask='true' width='400' height='200'><span>新增产品目录</span></a>";				
			}*/
			//在树的最后新增一个链接
			$containList[$i] = $containList[$i] . "<a href='__URL__/addCatalog/parentid/0/type_id/" .$typeList[$i]['id']. "'  target='dialog' mask='true' width='645' height='370'><span>新增一级产品目录</span></a>";
		}
		//dump($containList);
		$this->assign("containList",$containList);
		//$this->assign("temp_typeList",$temp_typeList);		
		$this->display(APP_PATH . '/Tpl/default/ProductCatalog/index.html');
	}
	
	/**
	 * 
	 * 分类明细查询
	 */
	public function detail(){
		$this->assign("id",$_REQUEST ['id']);		
		$this->assign("parentid",$_REQUEST ['parentid']);	
		$this->assign("type_id",$_REQUEST ['type_id']);	
		
		//封装查询参数
		$queryCondition[0] = 'parentid' ;
		$queryCondition[1] = 'type_id' ;
		//$queryCondition[2] = 'status' ;
		
		$this->formQuery("viewproductcatalog",$queryCondition) ;
		$this->display ();
		return;	
	}
	

	private function getSubFolder($path,$type,$index){
		$str = "" ;		
		$subFolderCondition['childid'] = array('NEQ','0');
		$subFolderCondition['parentid'] = $path ;	
		$subFolderCondition['type_id'] = $type ;
		//$subFolderCondition['status'] = 1 ;
		$subFolderList = $this->CommonSimplequery($subFolderCondition,'product_catalog');
		for ($i= 0;$i< count($subFolderList); $i++){				
			$tempList = $subFolderList[$i];
			//dump($tempList) ;						
			if($tempList['parentid'] == 0 ){				
				$str = $str . "<ul class='tree treeFolder'>" ;	
			}else{
				$str = $str . "<ul>" ;
			}				
			//$str = $str . "<li><a href='__URL__/detail/querytype/init/status/1/id/" . $tempList['id'] . "/parentid/" .$tempList['id']. "/type_id/" .$tempList['type_id']. "' target='navTab' title = '分类明细' rel='".($index+1) ."'>  " . $tempList['name']  . " </a>"  ;
			if($tempList['status'] == 0){
				$str = $str . "<li><a href='__URL__/addCatalog/parentid/" . $tempList['id'] . "/type_id/" . $tempList['type_id']. "' target='dialog' title = '分类明细' width='645' height='370' rel='".($index+1) ."'> <img src='__PUBLIC__/images/treeadd.png' >" . $tempList['name']  . "</img> </a>"  ;
			}else{
				$str = $str . "<li><a href='__URL__/addCatalog/parentid/" . $tempList['id'] . "/type_id/" . $tempList['type_id']. "' target='dialog' title = '分类明细' width='645' height='370' rel='".($index+1) ."'>  " . $tempList['name']  . " </a>"  ;	
			}
			
			//遍历子结点		
					
			$str = $str . $this->getSubFolder($tempList['id'],$type,$index) ;
  			$str = $str . $this->getSubDirect($tempList['id'],$type,$index) ;
			$str = $str . "</li>" ;		
			$str = $str . "</ul>" ;				
		} 		
		return $str;		
	}
	
	private function getSubDirect($path,$type){
		$str = "" ;
		$subDirectCondition['childid'] = '0';
		$subDirectCondition['parentid'] = $path ;
		$subFolderCondition['type_id'] = $type ;
		//$subFolderCondition['status'] = 1 ;
		$subDirectList = $this->CommonSimplequery($subDirectCondition,'product_catalog');
		for ($i= 0;$i< count($subDirectList); $i++){
			$tempList = $subDirectList[$i];				
			$str = $str . "<ul>" ;
			//$str = $str . "<li><a href='__URL__/detail/status/1/id/" . $tempList['id'] . "/parentid/" .$tempList['parentid']. "/type_id/" .$tempList['type_id']. "' target='navTab' title = '分类明细' rel='".($index+1)."'>" . $tempList['name'] . "</a>" ;				
			//$str = $str . "<li><a href='__URL__/detail/id/" . $tempList['id'] . "/parentid/" .$tempList['parentid']. "/type_id/" .$tempList['type_id']. "' target='navTab' width='645' height='370' title = '分类明细' rel='".($index+1)."'>" . $tempList['name'] . "</a>" ;
			$str = $str . "<li><a href='__URL__/addCatalog/parentid/" . $tempList['id'] . "/type_id/" . $tempList['type_id']. "' target='dialog' title = '分类明细' width='645' height='370' rel='".($index+1) ."'>  " . $tempList['name']  . " </a>"  ;
			$str = $str . "</li>" ;
			$str = $str . "</ul>" ;
		}
		return $str;		
	}	
	
	/**
	 * 
	 * 新增产品目录操作
	 */
	public function addCatalog(){		
		
		
		$this->assign("parentid",$_REQUEST ['parentid']);	
		$this->assign("type_id",$_REQUEST ['type_id']);	
		
		$catalog_condition['id'] = $_REQUEST ['parentid'] ;
		$this->assign("editCatalog",$this->CommonSimplequery($catalog_condition,'product_catalog',1)) ;
		$this->display();
	}
	
	/**
	 * 
	 * 保存产品目录
	 */
	public function saveCatalog(){
		$model = D( $this->table );	
		$data["name"] = $_REQUEST ['name'];		
		$data["childid"] = 1;
		$data["createdate"] = date('Y-m-d H:i:s',time());
		$data["statusdate"] = date('Y-m-d H:i:s',time());
		$data["status"] = $_REQUEST ['status'];		
		$data["parentid"] = $_REQUEST ['parentid'];		
		$data["type_id"] = $_REQUEST ['type_id'];
		//$this->resultHandle($model->add($data));
		
		//保存图片
		$imgName = $this->_upload();		
		$data["imageurl_small"] = "s_".$imgName ;
		$data["imageurl_big"] = "m_".$imgName ;	
		$save_result = $model->add($data) ;		
		$type_model = D("product_type");
		$update_data['statusdate'] =  date('Y-m-d H:i:s',time());
		$update_condition['id'] = $_REQUEST ['type_id'] ; 
		$update_result = $type_model->where($update_condition)->save($update_data);		
		$this->resultHandle($save_result,"新增产品目录成功！","新增产品目录失败！");

	}
	
	/**
	 * 重载函数
	 * @see Admin/Lib/Action/CommonAction::getReturnUrl()
	 */
	function getCatalogReturnUrl() {
		return __URL__ . '?' .  C ( 'VAR_MODULE' ) . '=' . MODULE_NAME . '&' . C ( 'VAR_ACTION' ) . '=' . C ( 'DEFAULT_CATALOG_ACTION' );
		
	}

	
	public function edit(){		
		$condition['id'] = $_REQUEST['id'];		
		$this->assign( "productCatalogVo" , $this->CommonSimplequery($condition, "product_catalog",1));
		$this->display();
	}
	
	
	/**
	 * 
	 * 修改产品分类
	 */
	public function editProductCatalog(){
		$model = D( "product_catalog" );			
		$condition['id'] = $_REQUEST ['id'];		
		$vo = $this->CommonSimplequery($condition, "product_catalog",1);
		$data['status'];
		if($vo['status'] == 1){
			$data['status'] = 0  ;
		}else{
			$data['status'] = 1  ;
		}
		
		if( $_FILES['image']['name'] != ''){				
			$imgName = $this->_upload();
			$data["imageurl_small"] = "s_".$imgName ;
			$data["imageurl_big"] = "m_".$imgName ;
		}
		$data["statusdate"] = date('Y-m-d H:i:s',time());			
		$data['name'] = $_REQUEST ['name'];		
   	    $this->resultHandle($model->where($condition)->save($data),"产品分类信息修改成功！","产品分类信息修改失败！") ;
	}

}
?>